﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NXDO.Data.Extension;
using NXDO.WebAPI.Controllers;
using System;
using System.IO;
using TDM.DbData.DataEntity;
using TDM.WebBase;
using TDM.WebBiz.TestData;

namespace TDM.WebSvr.Controllers.TestData
{
    [AllowAnonymous]
    public class DataImportController : ApiDbEntityController<DataImport,long>
    {
        protected override string GetPkName()
        {
            return "ImportID";
        }

        protected override string GetTableName()
        {
            return "DataImport";
        }

        public override bool Delete(long pkValue)
        {
            var dataImport = DataImportHelper.Get(pkValue);
            if (dataImport == null) throw new Exception("未找到需要删除的数据");
            try
            {
                string basePath = BasePathHelper.GetDataImportPath();
                string fullpath = Path.Combine(basePath, dataImport.FilePath);
                if (System.IO.File.Exists(fullpath)) System.IO.File.Delete(fullpath);
            }
            catch (Exception)
            {
            }
            return base.Delete(pkValue);
        }
        /// <summary>
        /// Data导入
        /// </summary>
        /// <param name="ImportID">选中的导入Data文件主键</param>
        /// <param name="isDelete">是否删除旧数据</param>
        [HttpGet]
        public void ExeImportData(long ImportID, bool isDelete)
        {
            try
            {
                DataImportHelper.ImportData(ImportID, isDelete);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
